package com.jin.crud.controller;


import com.github.pagehelper.PageInfo;
import com.jin.crud.bean.QueryDto;
import com.jin.crud.bean.QueryUser;
import com.jin.crud.bean.SysRole;
import com.jin.crud.bean.SysUserRole;
import com.jin.crud.common.utils.ResultUtil;
import com.jin.crud.service.SysRoleService;
import com.jin.crud.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author yuxuejin
 * @since 2021-11-16
 */
@Api("角色管理")
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    SysRoleService roleService;

    @Autowired
    private SysUserService userService;

    @ApiOperation("分页查询角色")
    @GetMapping("/getAllRole")
    public ResultUtil<PageInfo<SysRole>> getAllRole(QueryDto queryDto) {
        return ResultUtil.resultSuccess(roleService.getAllRolePage(queryDto));
    }

    @ApiOperation("通过ID查询角色")
    @GetMapping("/getRole")
    public ResultUtil<SysRole> getRole(@RequestParam("roleId") String roleId) {
        return ResultUtil.resultSuccess(roleService.getRole(roleId));
    }

    @ApiOperation("新增角色")
    @PostMapping("/addRole")
    public ResultUtil addRole(SysRole role) {
        roleService.addRole(role);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("修改角色")
    @PostMapping("/updateRole")
    public ResultUtil updateRole(SysRole role) {
        roleService.updateRole(role);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("删除角色")
    @PostMapping("/delRole")
    public ResultUtil delRole(@RequestParam("roleId") String roleId) {
        roleService.delRole(roleId);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("修改角色状态")
    @PostMapping("/updateRoleStatus")
    public ResultUtil updateRoleStatus(@RequestParam("roleId") String roleId, @RequestParam("status") String status) {
        roleService.updateRoleStatus(roleId, status);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("查询已分配用户角色列表")
    @GetMapping("/authUser/allocatedList")
    public ResultUtil allocatedList(QueryUser param) {
        return ResultUtil.resultSuccess(userService.selectAllocatedList(param));
    }

    @ApiOperation("查询未分配用户角色列表")
    @GetMapping("/authUser/unallocatedList")
    public ResultUtil unallocatedList(QueryUser param) {
        return ResultUtil.resultSuccess(userService.selectUnallocatedList(param));
    }

    @ApiOperation("取消授权用户")
    @PostMapping("/authUser/cancel")
    public ResultUtil cancelAuthUser(SysUserRole userRole) {
        roleService.deleteAuthUser(userRole);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("批量选择用户授权")
    @PostMapping("/authUser/cancelAll")
    public ResultUtil cancelAuthUserAll(String roleId, String[] userIds) {
        roleService.deleteAuthUsers(roleId, userIds);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("批量授权用户")
    @PostMapping("/authUser/selectAll")
    public ResultUtil selectAuthUserAll(String roleId, String[] userIds) {
        roleService.insertAuthUsers(roleId, userIds);
        return ResultUtil.resultSuccess();
    }
}

